package com.nuedu.corejava.ch10;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class ListClass {
	public static void main(String[] args) {
		//List集合
		//用于存放有序的数据，list集合的元素可能重复
		List list = new ArrayList();
		
		Computer computer = new Computer("HP", 5000);
		Computer computer2 = new Computer("HW", 5500);
		Computer computer3 = new Computer("HW2", 5500);
		
		//新增操作
		list.add(computer);
		list.add(computer2);
		list.add(1,computer3);//数据插入
		list.add(computer2);//list可以把一个对象保存到集合中，可以保存多次即该对象可以重复
		System.out.println("size: "+list.size());
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {// hasNext判断集合中是否有下一条记录
			Object object = iterator.next();// 取出数据，并把指针下移
			Computer comp = (Computer) object;
			System.out.println(comp);

		}
		
		//使用for循环迭代集合
		for (int i = 0; i < list.size(); i++) {
			Computer computer4 =(Computer) list.get(i);
			System.out.println(computer4);
		}
		
		System.out.println("====================================");
		//删除可以删除对象或根据索引进行删除
		list.remove(0);
		list.remove(computer2);
		//集合如果不指定数据类型，则可以保存任何引用数据类型
		list.add(new String("你好,集合"));
		
		list.add(new Date());
		
		//for each
		for (Object object : list) {
			System.out.println(object);
			//在读取数据时先进行判断
			if (object instanceof String) {
				String str = (String)object;
			}else if (object instanceof Computer) {
				Computer comp = (Computer)object;
			}else if (object instanceof Date) {
				Date date = (Date) object;
			}
			
		}
		
		
		
	}

}
